var Define = require("../../lib/Define.js")

var _wareId = 0
var _phone = 0
var _mark = ""
var _userInfo = null;


Page({
  data: {
    phone: ""
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    var self = this
    _wareId = options.wareId

    _userInfo = Define.getStoredUserInfo()
    _phone = _userInfo.phone
    console.log(_userInfo)
    self.setData({
      phone: _phone
    })
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成

  },
  bindPhoneInput: function (e) {
    //绑定手机号输入
    _phone = e.detail.value
  },
  bindMarkArea: function (e) {
    //绑定备注输入
    _mark = e.detail.value
  },
  onSubmitBtnClick: function () {
    //提交
    var self = this

    if (!_phone) {
      wx.showModal({
        content: '请输入您的手机号，以保证工作人员能联系到您',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    }

    Define.updateUserPhone(_phone)

    this.reqSaveOrder()
  },
  reqSaveOrder: function () {
    var param = {};
    param.userId = _userInfo.id
    param.phone = _phone
    param.wareId = _wareId
    param.bid = Define.value.bid
    if (_mark) {
      param.mark = _mark
    }
    wx.request({
      url: Define.value.baseUrl + "/app/order/add",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: param,
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        var data = res.data
        if (data.code == 200) {
          wx.navigateTo({
            url: '../ware/wareDetail/success/success?wareId=' + _wareId
          })
        } else {
          Define.alert(data.msg)
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  }
})